<?php
class CMRPageApproveResource extends CMRPage
{
	private $resourceID;
	
	public function __construct($database)
	{
		parent::__construct($database);
		if (isset($_POST['ResourceID']))
		$this->resourceID = $_POST['ResourceID'];
/*		if($this->getAction()=='UpdateRecord')
		{
			$callback = new DataTableCallback($this->Database->Recopy(), $this->UserID);
			$callback->updateRecord($_POST['DBTableName']);
		}
*/
		}
	


protected function getResources()
	{
		$resources = array();
		
		$sql = "SELECT * FROM Resources WHERE DeletedBy = '0' AND StatusTypeID = '2' ORDER BY ResourceTitle";
		$this->Database->ExecuteSQL($sql);
		while($row = $this->Database->getRow())
		{
			$resource = new Resource($this->Database->Recopy(),$row['ResourceID']);
//			$resource->loadData($row['ResourceID'],$row['ResourceTitle'],$row['ResourceDescription'],$row['ResourceTypeID'],"","",,,,);
			$resource->setDisplayMode('approve');
			$resource->setCurrentUserGroup($this->userGroup);
//			echo "Submit<br>\n";
			array_push($resources,$resource);
		}
				
		return $resources;
	}

	
	protected function getContentsDiv()
	{
		
		$category = new Hidden();
		$category->setName("ResourceID");
		$html = $category->getHTML();
	
		$html .= "<div id=\"content\">\n";
		$html .= "<h1>Approve a Resource</h1>";

		$hidden = new Hidden();
		$hidden->setName("SubAction");
		$html .= $hidden->getHTML();
		
		$script = new EditDeleteFormScript();
		$html .= $script->getHTML();
		
		switch ($this->getSubAction())
		{
			case 'ApproveResourcePreview':
				$html .= $this->getApproveResourcePreviewHTML();
				break;
				
			case 'ConfirmApproveResource':
				
				// add to actions and change status to submitted
				$date = new DateTime();
				$sql = "INSERT INTO Actions (ResourceID, ActionTypeID, ActionDate, ActionUserID, ActionComment) ";
				$sql .= "VALUES ('".$this->resourceID."','3','".$date->format('Y-m-d H:i:s')."','".$this->UserID."','".$_POST['ActionComment']."')";
//				echo $sql."<br>";
				$this->Database->ExecuteSQL($sql);

/*****************************************************************************************************************************************/
//get userIds for Fake1 and Fake2
				$sql = "SELECT UserID FROM Users WHERE LongName = 'Fake1'";
				$this->Database->ExecuteSQL($sql);
				$row = $this->Database->getRow();
				$fakeID1 = $row['UserID'];
				
				$sql = "SELECT UserID FROM Users WHERE LongName = 'Fake2'";
				$this->Database->ExecuteSQL($sql);
				$row = $this->Database->getRow();
				$fakeID2 = $row['UserID'];
				
// Insert with Fake1
				$sql = "INSERT INTO Actions (ResourceID, ActionTypeID, ActionDate, ActionUserID, ActionComment) ";
				$sql .= "VALUES ('".$this->resourceID."','3','".$date->format('Y-m-d H:i:s')."','".$fakeID1."','".$_POST['ActionComment']."')";
//				echo $sql."<br>";
				$this->Database->ExecuteSQL($sql);
				
// Insert with Fake2
				$sql = "INSERT INTO Actions (ResourceID, ActionTypeID, ActionDate, ActionUserID, ActionComment) ";
				$sql .= "VALUES ('".$this->resourceID."','3','".$date->format('Y-m-d H:i:s')."','".$fakeID2."','".$_POST['ActionComment']."')";
//				echo $sql."<br>";
				$this->Database->ExecuteSQL($sql);
				
/*****************************************************************************************************************************************/
				
				
				// can it go live??
				$sql = "SELECT DISTINCT id.ActionUserID, n.ShortName FROM Actions as id INNER JOIN Users as n ON id.ActionUserID = n.UserID WHERE id.ResourceID = ".$_POST['ResourceID']." AND id.ActionTypeID = 3";
//				echo $sql."<br>\n";
				$this->Database->ExecuteSQL($sql);
//				echo $this->Database->getNumRows()."<br>\n";
				$nApprovers = $this->Database->getNumRows();
				$approvers = "Resource approved by ";
					while($row = $this->Database->getRow())
					{
						$approvers .= $row['ShortName']." ";
					}
				if($nApprovers >= $this->NeededForApproval)
				{
					$sql = "UPDATE Resources SET StatusTypeID='3' WHERE ResourceID=".$this->resourceID;
					$this->Database->ExecuteSQL($sql);
					$html .= "<p>Resource is now live</p>";
				}	
			
				$html .= "<p>".$approvers."</p>";
				break;
				
			default:
				$html .= $this->getDefault();			
				break;
		}
		

		$html .= "</div>";
		return $html;
				
	}
	
	private function getDefault()
	{
		$html = "<p>Select the resource you wish to approve from the  list below.</p>";

		$resources = $this->getResources();
		foreach ($resources as $resource)
		{
			$html .= $resource->getSummaryHTML();
		}
				
/*		$button = new JavascriptButton();
		$button->setValue("Next");
		$button->setJavascript("var submitaction; submitaction='submitCreate'+(document.getElementById('SubmitType').options[document.getElementById('SubmitType').selectedIndex].text); document.getElementById('Action').value = 'adminCreateResource'; document.getElementById('SubAction').value = submitaction; document.getElementById('PageForm').submit();");
		$html .= $button->getHTML();
		*/
		return $html;
		
	}

	private function getApproveResourcePreviewHTML()
	{
		$this->resourceID = $_POST['ResourceID'];
		$resource = new Resource($this->Database, $this->resourceID);
		$html = $resource->getDataHTML();
//		echo "Resource type = ".$resource->getResourceTypeID();
		switch ($resource->getResourceTypeID())
		{
			case 2: // Repertoire item
				$resource = new ResourceRepertoireInfo($this->Database->Recopy(),$this->resourceID);
//				$html .= $resource->getDataHTML();
				$html .= $resource->getFullHTML();
				break;
				
			case 3: // Composer
				$resource = new ResourceComposer($this->Database->Recopy(),$this->resourceID);
//				$html .= $resource->getDataHTML();
				$html .= $resource->getFullHTML();
				break;
				
			case 6: // File
				$resource = new ResourceFile($this->Database->Recopy(),$this->resourceID);
				$html .= $resource->getDataHTML();
				break;
				
			case 7: // link
				$resource = new ResourceLink($this->Database->Recopy(),$this->resourceID);
				$html .= $resource->getDataHTML();
				break;
				
			default:
				break;
		}
		$html .= "<hr>\n";
		$ActionComment = new TextArea();
		$ActionComment->setName('ActionComment');
		$ActionComment->setRows(20);
		$ActionComment->setColumns(40);
		$html .= "\n<table><tr><td>Comment</td><td>".$ActionComment->getHTML()."</td></tr></table>\n";
		
		$button = new JavaScriptButton();
		$button->setValue("Confirm Approval");
		$button->setJavascript("document.getElementById('ResourceID').value = '".$this->resourceID."'; document.getElementById('Action').value = 'adminApproveResource'; document.getElementById('SubAction').value = 'ConfirmApproveResource'; document.getElementById('PageForm').submit();");
		$html .= $button->getHTML();
						
		return $html;
	}
	
	
}
?>